using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace Geekees.App.Cashbook.Web
{
	/// <summary>
	/// Summary description for Playground.
	/// </summary>
	public class Playground : System.Web.UI.Page
	{
		private void Page_Load(object sender, System.EventArgs e)
		{
			DateTime dt = DateTime.Now;

			Response.Write( "first day of week:" + GetFirstDayOfWeek(dt).ToString() + " last day of week: " + GetLastDayOfWeek(dt).ToString() + "<br />");
			Response.Write("first day of month:" + GetFirstDayOfMonth( dt ).ToString() + "last day of month: "+ GetLastDayOfMonth( dt ).ToString() + "<br />");
			
		}

		#region Date Methods

		private DateTime GetFirstDayOfWeek( DateTime dtDate )
		{
			return dtDate.AddDays( - (int)dtDate.DayOfWeek );
		}

		private DateTime GetLastDayOfWeek( DateTime dtDate )
		{
			return dtDate.AddDays( 6 - (int)dtDate.DayOfWeek );
		}

		/// <summary>
		/// Get the first day of the month for
		/// any full date submitted
		/// </summary>
		/// <param name="dtDate"></param>
		/// <returns></returns>
		private DateTime GetFirstDayOfMonth(DateTime dtDate)
		{
			// set return value to the first day of the month
			// for any date passed in to the method
 
			// create a datetime variable set to the passed in date
			DateTime dtFrom = dtDate;
			// remove all of the days in the month
			// except the first day and set the
			// variable to hold that date
			dtFrom = dtFrom.AddDays(-(dtFrom.Day - 1));
 
			// return the first day of the month
			return dtFrom;
		}
        
		/// <summary>
		/// Get the first day of the month for a
		/// month passed by it's integer value
		/// </summary>
		/// <param name="iMonth"></param>
		/// <returns></returns>
		private DateTime GetFirstDayOfMonth(int iMonth)
		{
			// set return value to the last day of the month
			// for any date passed in to the method
 
			// create a datetime variable set to the passed in date
			DateTime dtFrom = new DateTime(DateTime.Now.Year, iMonth, 1);
 
			// remove all of the days in the month
			// except the first day and set the
			// variable to hold that date
			dtFrom = dtFrom.AddDays(-(dtFrom.Day - 1));
 
			// return the first day of the month
			return dtFrom;
		}
  
		/// <summary>
		/// Get the last day of the month for any
		/// full date
		/// </summary>
		/// <param name="dtDate"></param>
		/// <returns></returns>
		private DateTime GetLastDayOfMonth(DateTime dtDate)
		{
			// set return value to the last day of the month
			// for any date passed in to the method
 
			// create a datetime variable set to the passed in date
			DateTime dtTo = dtDate;
 
			// overshoot the date by a month
			dtTo = dtTo.AddMonths(1);
 
			// remove all of the days in the next month
			// to get bumped down to the last day of the
			// previous month
			dtTo = dtTo.AddDays(-(dtTo.Day));
 
			// return the last day of the month
			return dtTo;
		}
  
		/// <summary>
		/// Get the last day of a month expressed by it's
		/// integer value
		/// </summary>
		/// <param name="iMonth"></param>
		/// <returns></returns>
		private DateTime GetLastDayOfMonth(int iMonth)
		{
 
			// set return value to the last day of the month
			// for any date passed in to the method
 
			// create a datetime variable set to the passed in date
			DateTime dtTo = new DateTime(DateTime.Now.Year, iMonth, 1);
 
			// overshoot the date by a month
			dtTo = dtTo.AddMonths(1);
 
			// remove all of the days in the next month
			// to get bumped down to the last day of the
			// previous month
			dtTo = dtTo.AddDays(-(dtTo.Day));
 
			// return the last day of the month
			return dtTo;
 
		}
 
		#endregion

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{    
			this.Load += new System.EventHandler(this.Page_Load);
		}
		#endregion
	}
}
